package com.movie.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.movie.datacomm.common.CommResp;
import com.movie.datacomm.model.Hall;
import com.movie.datacomm.model.Movie;
import com.movie.datacomm.service.HallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("hall")
public class HallController {

    @Autowired
    HallService hallService;


    @RequestMapping(value = "/getCurrentPageHall", method = RequestMethod.GET)
    @ResponseBody
    public CommResp getCurrentPageHall(int currentPage, int pageSize, @Nullable String input){

        Page<Hall> page = new Page<>(currentPage, pageSize);

        QueryWrapper<Hall> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(input)){
            wrapper.like("hall_name", input);
        }
        wrapper.orderByAsc("hall_id");

        page = hallService.page(page, wrapper);
        return CommResp.okList(page.getRecords(), page.getTotal());
    }

    @RequestMapping(value = "/updateHallInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommResp updateHallInfo(@RequestBody Hall hall){
        boolean result = hallService.updateById(hall);
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("更新失败");
        }
    }

    @RequestMapping(value = "/addHallInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommResp addHallInfo(@RequestBody Hall hall){

        boolean result = hallService.save(hall);
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("操作失败");
        }
    }

    @RequestMapping(value = "/deleteCurrentHall", method = RequestMethod.POST)
    @ResponseBody
    public CommResp deleteHallInfo(@RequestBody Hall hall){

        boolean result = hallService.removeById(hall.getHallId());
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("删除失败");
        }
    }


}
